package com.heima.search.listener;

import com.heima.search.service.ApArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.heima.common.constants.message.Article2EsConstants.ARTICLE_2_ES_TOPIC;

@Slf4j
@Component
public class ArticleListener {

    @Autowired
    private ApArticleSearchService articleSearchService;

    @KafkaListener(topics = ARTICLE_2_ES_TOPIC)
    public void receiveMsg(ConsumerRecord<?,?> record){
        //判断消息是否有值
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            //获取value值  文章id
            Object value = record.value();
            Long articleId = Long.parseLong(value.toString());
            articleSearchService.ImportOneArticle2Es(articleId);
        }
    }


}
